package cn.sfa.controller.users;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.sfa.common.pagination.Pagination;
import cn.sfa.common.utils.CommonUtil;
import cn.sfa.entity.users.SaleUsersLogin;
import cn.sfa.service.messagemanager.MessageManagerService;

@Controller
@RequestMapping("/users/mess/index")
public class MessageManagerController {
	Logger log = LoggerFactory.getLogger(MessageManagerController.class);

	@Autowired
	MessageManagerService messageService;

	private Pagination pagination;

	public Pagination getPagination() {
		return pagination;
	}

	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}

	@RequestMapping("/querymess")
	public String querymessage(ModelMap model, HttpServletRequest request) {
		int currentPage = 1;
		if (!CommonUtil.isEmpty(request.getParameter("currentPage"))) {
			currentPage = Integer.parseInt(request.getParameter("currentPage"));
		}
		SaleUsersLogin user = (SaleUsersLogin) request.getSession()
				.getAttribute("user");
		String url = request.getContextPath() + "/users/mess/index/querymess";
		pagination = messageService.GetMessageBaseList(currentPage, 100, url,
				user.getUserAgentId());
		model.put("pagination", pagination);
		return "message/mess/messagelist";
	}
	
	
	@RequestMapping("/setting")
	public String setting(ModelMap model, HttpServletRequest request) {
		 
		return "message/mess/setting";
	}

	/*@RequestMapping("/editmess1")
	public String editmess1(
			ModelMap model,
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "baseid", required = false) String BaseId,
			@RequestParam(value = "subscribe", required = false) String Subscribe) {
		SaleUsersLogin user = (SaleUsersLogin) request.getSession()
				.getAttribute("user");
		messageService.EditMessageBaseAgentById(user
				.getUserAgentId(), BaseId, Subscribe);
		return "message/mess/messagelist";
	}*/

	@RequestMapping("/editmess")
	public void editmess(
			ModelMap model,
			HttpServletRequest request,
			HttpServletResponse response,
	
			@RequestParam(value = "baseid", required = false) String BaseId,
			@RequestParam(value = "type", required = false) String type,
			@RequestParam(value = "subscribe", required = false) String Subscribe
		) throws Exception {

		try {
			response.setContentType("text/html");
			PrintWriter out;
			out = response.getWriter();
			SaleUsersLogin user = (SaleUsersLogin) request.getSession()
					.getAttribute("user");
			String returnData = messageService.EditMessageBaseAgentById(user
					.getUserAgentId(), BaseId, Subscribe,type);
			out.print(returnData);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
